package com.project.weimi.fragment;

import com.project.weimi.R;
import com.project.weimi.activity.AboutUsActivity;
import com.project.weimi.activity.AddressManagerActivity;
import com.project.weimi.activity.CommonSettingActivity;
import com.project.weimi.activity.CouponActivity;
import com.project.weimi.activity.IntegralShopActivity;
import com.project.weimi.activity.LoginActivity;
import com.project.weimi.activity.MyCollectionActivity;
import com.project.weimi.activity.MyGiftActivity;
import com.project.weimi.activity.MyOrderActivity;
import com.project.weimi.activity.PersonalInfoActivity;
import com.project.weimi.activity.RegisterActivity;
import com.project.weimi.activity.ServiceCenterActivity;
import com.project.weimi.activity.WeiXinActivity;
import com.project.weimi.utils.SetClickListener;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class MineFragment extends BaseFragment {

	private ImageView imgBtn_set;
	private RelativeLayout jifen_relativeLayout, gift_relativeLayout, collect_relativeLayout, coupon_relativeLayout,
			order_relativeLayout, address_relativeLayout, aboutUs_relativeLayout, weixin_relativeLayout,
			service_relativeLayout;
	
	// 未登录的头部布局
	private LinearLayout layout_unLogin;
	private Button btn_login,btn_register;
	// 登录后的头部布局
	private RelativeLayout layout_alreadyLogin;
	private TextView tv_nickName,tv_sex;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// 解析布局文件
		View view = inflater.inflate(R.layout.fragment_mine, null);

		// 获取控件id
		initView(view);

		return view;
	}

	private void initView(View view) {
		imgBtn_set = (ImageView) view.findViewById(R.id.imgBtn_set);
		jifen_relativeLayout = (RelativeLayout) view.findViewById(R.id.jifen_relativeLayout);
		gift_relativeLayout = (RelativeLayout) view.findViewById(R.id.gift_relativeLayout);
		collect_relativeLayout = (RelativeLayout) view.findViewById(R.id.collect_relativeLayout);
		coupon_relativeLayout = (RelativeLayout) view.findViewById(R.id.coupon_relativeLayout);
		order_relativeLayout = (RelativeLayout) view.findViewById(R.id.order_relativeLayout);
		address_relativeLayout = (RelativeLayout) view.findViewById(R.id.address_relativeLayout);
		aboutUs_relativeLayout = (RelativeLayout) view.findViewById(R.id.aboutUs_relativeLayout);
		weixin_relativeLayout = (RelativeLayout) view.findViewById(R.id.weixin_relativeLayout);
		service_relativeLayout = (RelativeLayout) view.findViewById(R.id.service_relativeLayout);
		layout_alreadyLogin = (RelativeLayout) view.findViewById(R.id.layout_alreadyLogin);
		layout_unLogin = (LinearLayout)view.findViewById(R.id.layout_unLogin);
		btn_login = (Button)view.findViewById(R.id.btn_login);
		btn_register = (Button)view.findViewById(R.id.btn_register);
		tv_nickName = (TextView)view.findViewById(R.id.tv_nickName);
		tv_sex = (TextView)view.findViewById(R.id.tv_sex);
		
		// 监听
		SetClickListener.setClickListener(listener, jifen_relativeLayout, collect_relativeLayout,
				coupon_relativeLayout, order_relativeLayout, address_relativeLayout, aboutUs_relativeLayout,
				weixin_relativeLayout, service_relativeLayout,btn_login,btn_register,layout_alreadyLogin,
				imgBtn_set);

	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = null;
			
			switch (v.getId()) {
			case R.id.jifen_relativeLayout: //跳转到“积分商城”页面
				intent = new Intent(getActivity(),IntegralShopActivity.class);
				break;
//			case R.id.gift_relativeLayout: //跳转到“我的礼物”页面
//				intent = new Intent(getActivity(),MyGiftActivity.class);
//				break;
			case R.id.collect_relativeLayout: //跳转到“我的收藏”页面
				intent = new Intent(getActivity(),MyCollectionActivity.class);
				break;
			case R.id.coupon_relativeLayout: //跳转到“优惠券”页面
				intent = new Intent(getActivity(),CouponActivity.class);
				break;
			case R.id.order_relativeLayout: //跳转到“订单”页面
				intent = new Intent(getActivity(),MyOrderActivity.class);
				break;
			case R.id.address_relativeLayout: //跳转到“地址管理”页面
				intent = new Intent(getActivity(),AddressManagerActivity.class);
				break;
			case R.id.aboutUs_relativeLayout: //跳转到“关于我们”页面
				intent = new Intent(getActivity(),AboutUsActivity.class);
				break;
			case R.id.weixin_relativeLayout: //跳转到“关注微信”页面
				intent = new Intent(getActivity(),WeiXinActivity.class);
				break;
			case R.id.service_relativeLayout: //跳转到“客服”页面
				intent = new Intent(getActivity(),ServiceCenterActivity.class);
				break;
			case R.id.btn_login: //跳转到“登录”页面
				intent = new Intent(getActivity(),LoginActivity.class);
				break;
			case R.id.btn_register: //跳转到“注册”页面
				intent = new Intent(getActivity(),RegisterActivity.class);
				break;
			case R.id.layout_alreadyLogin: //跳转到“个人资料”页面
				intent = new Intent(getActivity(),PersonalInfoActivity.class);
				break;
			case R.id.imgBtn_set: // 跳转到“通用设置”页面
				intent = new Intent(getActivity(),CommonSettingActivity.class);
				break;
			}
			startActivity(intent);
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		if(null != spHelper && !TextUtils.isEmpty(memberId)){
			Log.e("TAG", "MineFragment====onResume==="+memberId);
			layout_alreadyLogin.setVisibility(View.VISIBLE);
			layout_unLogin.setVisibility(View.GONE);
			
			if(TextUtils.isEmpty(baseNickName)){
				tv_nickName.setText("昵称");
			} else {
				tv_nickName.setText(baseNickName);
			}
			
			if(TextUtils.isEmpty(baseSex)){
				tv_sex.setText("性别");
				tv_sex.setTextColor(Color.parseColor("#2D5DFB"));
				Log.e("TAG", "MineFragment====性别onResume空==="+baseSex);
			} else {
				tv_sex.setText(baseSex);
			}
			
			//Log.e("TAG", "MineFragment====onResume最后==="+baseSex);
			
		} else {
			layout_alreadyLogin.setVisibility(View.GONE);
			layout_unLogin.setVisibility(View.VISIBLE);
		}
	}
	
	
}
